<?
include_once("/var/www/html/new/com.inc.php");
include('../../jq/com/errBack.php');
include("../class/ProductInfo.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$pid=abs($_REQUEST['pid']);
$p=new ProductInfo($s,$pid);
$orderMonth=$_REQUEST['orderMonth'];
$str="select t.id as tid,t.storage,t.storage_open from uu_products p join uu_jq_ticket t on t.pid=p.id left join uu_land_f f on f.pid=p.id where p.id=$pid limit 1";
$GLOBALS['le']->query($str);
$p_info=$GLOBALS['le']->fetch_assoc();
$monthStart=$orderMonth."-01";
$monthEnd=$orderMonth."-".date("t",strtotime($monthStart));
$mintime=$p->getMinValidTime();
$maxtime=$p->getMaxValidTime();
$begintime=$mintime;
if($begintime<$monthStart){
	$begintime=$monthStart;
}
$endtime=$monthEnd;
if($begintime>$endtime){
	$begintime=$endtime;
}
//剩余总库存
$storageXml=$s->Dynamic_Storage_Check($p_info['tid'],2,NUll,$p_info['storage_open']);
if($p_info['storage']==-1){
	$totalStorage=-1;
}else{
	$storageXml=simplexml_load_string($storageXml);
	$useStorage=$storageXml->Rec->UUXstr;
	$totalStorage=$p_info['storage']-$useStorage;
}
$prices=$p->getPeriodPrice($begintime,$endtime);
$GLOBALS['le']->connectsp();
$jsonData=array();
$dstorage=array();
$tid=$p_info['tid'];
for($i=0,$days=ceil((strtotime($endtime)-strtotime($begintime))/3600/24)+1;$i<$days;$i++){
	$date=date("Y-m-d",strtotime($begintime)+$i*24*3600);
	$le=new go_sql();
	$GLOBALS['le']->connectsp();
	$qstr="CALL GSTORAGE('$date',$tid)";
	$GLOBALS['le']->querysp($qstr);
	$row=$GLOBALS['le']->fetch_arraysp();
	$dstorage[$date]=$GLOBALS['le']->f("cc");
	$GLOBALS['le']->close();

}
foreach($prices as $date=>$price){
	$remain=$totalStorage;
	if($price['storage']!=-1){
		$remain=$price['storage']-$dstorage[$date];
	}
	if($totalStorage!=-1){
		$remain=min((float)$remain,(float)$totalStorage);
	}
	$jsonData[]=array("date"=>$date,"sprice"=>$price['sprice']/100,"lprice"=>$price['lprice']/100,"uid"=>$price['uid'],"storage"=>$price['storage'],"sale"=>$dstorage[$date],"remain"=>$remain);
}
$data["pricelists"]=$jsonData;
$data["about"]["mintime"]=strtotime($mintime);
$data["about"]["maxtime"]=strtotime($maxtime);
$data["about"]["totalStorage"]=$totalStorage;
if($_REQUEST['datatype']=="jsonp"){
	echo $_REQUEST['callback']."(".json_encode($data).")";
}else{
	echo json_encode($data);
}
?>